#!/bin/bash

ifeq (,$(TOP_DIR))
TOP_DIR := $(shell pwd)/..
CURRENT_DIR := $(shell pwd)
else
CURRENT_DIR := $(TOP_DIR)/lib
endif

QTSM_LIB_SRC := $(CURRENT_DIR)/*.c
QTSM_IO_SRC := $(TOP_DIR)/io/*.c

QTSM_LIB_OBJ := $(CURRENT_DIR)/*.o

INCLUDE_PATH := -I $(TOP_DIR)/include/

LIBS += -lcbor -lcrypto -lssl

LIB_TARGET := $(CURRENT_DIR)/libqtsm.so

ifeq (,$(CC))
CC := gcc
endif

CFLAGS := -W -Os -g -fPIC
CFLAGS += -Wl,-z,relro,-z,now
CFLAGS += -Wl,-z,noexecstack
CFLAGS += -fstack-protector-strong

LDFLAGS := -shared

qtsm_lib:
	@echo "---------------------------------------- Start Make QTSM Library -------------------------------------------"
	$(CC) $(CFLAGS) $(INCLUDE_PATH) -c $(QTSM_LIB_SRC)
	$(CC) $(CFLAGS) $(INCLUDE_PATH) -c $(QTSM_IO_SRC)
	${CC} $(LDFLAGS) -o $(LIB_TARGET) $(QTSM_LIB_OBJ) $(LIBS)
	cp $(LIB_TARGET) $(TOP_DIR)/output/
	@echo "---------------------------------------- End Make QTSM Library -------------------------------------------"

clean:
	rm *.o $(LIB_TARGET) -rf
	rm $(TOP_DIR)/output/libqtsm.so -rf
